import {Injectable} from '@angular/core';
import {Http,Headers,Response} from '@angular/http'
import {Project} from "../entity/Project";
import 'rxjs/add/operator/toPromise';
import {Page} from "../../shared/entity/Page";
import {CommonIp} from "../../user/entity/Ip";


@Injectable()
export class ProjectFindService extends CommonIp{

  private projectIndexListURL = this.userIp+"/p/project/1/recommendProject";
  private projectRankingListURL = this.userIp+"/p/project/1/rankingProject";
  private projectInfoURL = this.userIp+"/p/project/projectIndex/";
  private findProjectPagerByCategoryPrefix = this.userIp+"/p/project/";
  private findProjectPagerByCategorySuffix = "/projectPager";
  constructor(public http:Http){
    super();
  }
  public getProjectList(siteId:string):Promise<Project[]> {

    return this.http.get(this.projectIndexListURL)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  public getRankingProjectList(siteId: string):Promise<Project[]> {
    return this.http.get(this.projectRankingListURL)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);

  }

  public getProjectInfo(id: string):Promise<Project> {

    return this.http.get(this.projectInfoURL+id)
      .toPromise()
      .then(this.extractInfo)
      .catch(this.handleError);
  }


  private extractInfo(res:Response) {
    if(res.status<200||res.status>=300){
      throw  new Error('Bad response status:'+res.status);
    }
    let body = res.json();
    return body.entity||{};
  }
  private extractData(res:Response){
    if(res.status<200||res.status>=300){
      throw new Error('Bad response status:'+res.status);
    }
    let body  = res.json();

    return body.list||{};
  }


  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }


  public getProjectPageByCategory(categoryId: string,pageNo:string):Promise<Page<Project>> {

    sessionStorage.setItem("currentOneCategoryId",categoryId);
    return this.http.get(this.findProjectPagerByCategoryPrefix
      +categoryId+this.findProjectPagerByCategorySuffix+"?pageNo="+pageNo)
      .toPromise()
      .then(this.extractPageInfo)
      .catch(this.handleError);
  }

  private extractPageInfo(res:Response) {
    if(res.status<200||res.status>=300){
      throw  new Error('Bad response status:'+res.status);
    }
    let body = res.json();
    return body.page||{};
  }


  public getProjectPageByCategory1(categoryId: string,pageNo:string):Promise<Page<Project>> {
    if(document.body.clientWidth<450){
      $("#category-child-list").css("display","");
    }
    sessionStorage.setItem("currentOneCategoryId",categoryId);
    return this.http.get(this.findProjectPagerByCategoryPrefix
      +categoryId+this.findProjectPagerByCategorySuffix+"?pageNo="+pageNo)
      .toPromise()
      .then(this.extractPageInfo)
      .catch(this.handleError);
  }

}
